@echo off

REM
REM *******************************************************************
REM  A script to copy header files under Windows.
REM  Usage: win_copy_headers [output path]
REM *******************************************************************
REM

setlocal enabledelayedexpansion

set CPPFLIBSRC=cppflib
set FULLPATHSRCDIR=%~dp0%CPPFLIBSRC%\
set TARGETDIR=%1

if not "x%TARGETDIR%" == "x" goto :add_dir_sep
echo Usage: win_copy_headers.bat ^<target dir^>
goto:eof

:add_dir_sep
if "%TARGETDIR:~-1%" == "\" goto :make_target_dir
set TARGETDIR=%TARGETDIR%\

:make_target_dir
if exist %TARGETDIR% goto :chk_cppflib_dir
mkdir %TARGETDIR%

:chk_cppflib_dir
if exist %CPPFLIBSRC% goto :copy_headers
echo Can't find directory -- %CPPFLIBSRC% 
goto:eof

:copy_headers
REM
REM  %%~ff is full path
REM  %%~dpf is [drive:dir]
REM
REM  Loop through all .h files recursively
REM
for /r %CPPFLIBSRC% %%f in (*.h) do call:CopyHeaderFile %%~ff %%~dpf

echo Headers are successfully copied to %TARGETDIR%
endlocal

REM end of script
goto:eof

REM *******************************************
REM  Function to copy one header file
REM  param1 [in] full path of the source file
REM  param2 [in] full path of dir where the source file resides
REM *******************************************
:CopyHeaderFile

set srcDir=%~2
REM ignore "__internal" dir
if "%srcDir:~-11%" == "__internal\" goto ignore_copy
set targetFileDir=!srcDir:%FULLPATHSRCDIR%=%TARGETDIR%!
REM make target dir if not exists
if exist %targetFileDir% goto :real_copy_header
mkdir %targetFileDir%

:real_copy_header
set srcFilePath=%~1
REM really copy header here
set targetFilePath=!srcFilePath:%FULLPATHSRCDIR%=%TARGETDIR%!
copy /Y %srcFilePath% %targetFilePath%

:ignore_copy

goto:eof
